package com.soccerlover.strictfield.example;

import com.soccerlover.strictfield.annotation.CheckTarget;
import com.soccerlover.strictfield.annotation.ExcludeStrictCheck;
import com.soccerlover.strictfield.annotation.StrictField;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.annotation.Target;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hck soccerlover9527@gmail.com
 */
@RestController
@RequestMapping("/strict-field-example")
public class ExampleController {

    @PostMapping("getUserList")
    public List<User> getUserList(@RequestBody QueryParam queryParam) {
        // do something
        return new ArrayList<>();
    }

    /**
     * 查询参数
     */
    static class QueryParam {
        private int size;
        private int current;
        /**
         * 这里表示 orderBy 的合法值只能是 User 里的属性值.也就是可以为 id,name,gender,age
         */
        @StrictField(@CheckTarget(User.class))
        private String orderBy;

        public void setSize(int size) {
            this.size = size;
        }

        public void setCurrent(int current) {
            this.current = current;
        }

        public void setOrderBy(String orderBy) {
            this.orderBy = orderBy;
        }
    }

    /**
     * 用户实体
     */
    static class User {
        private Long id;
        private String name;
        private boolean gender;
        private int age;
        //  排除不支持排序的字段
        @ExcludeStrictCheck
        private String address;
    }
}
